<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class control_index extends base_control{

	//hook ctr_index_before.php

	public function __construct() {
	
		//hook ctr_index_parent_init_before.php
		parent::__construct();
		
		//hook ctr_index_parent_init_after.php
	}
	
	public function do_index() {
		//hook ctr_index_index_start.php
		
		$this->do_forumlist();
	}
	
	public function do_forumlist() {
		$forumlist_new_num = 12;
		//hook ctr_index_forumlist_start.php
		$allstick_count = $this->talks->talks_of_allstick_count();
		$all_sticks = $this->talks->talks_of_allstick_list(0, $allstick_count);
		
		$talks = $this->talks->talks_all_new_list(0, $forumlist_new_num);
		$forums = $this->forum->forum_list(0);
		//hook ctr_index_forumlist_before_asgn.php
	
		$this->tpl->assign('forums', $forums);
		$this->tpl->assign('talks', $talks);
		$this->tpl->assign('all_sticks', $all_sticks);
		$this->tpl->display('index_index.tpl');
	}
	
	public function do_forum() {
		//hook ctr_index_forum_start.php
		$fid = core::gpc('fid');
		$fid = max(0, intval($fid));
		
		$d = core::gpc('d','G');
		$digest = $d=='j' ? true : false;
		
		//hook ctr_index_forum_before_forum_get.php
		$bbs = $this->forum->forum_get($fid);
		
		//hook ctr_index_forum_before_limit_check.php
		if(!$this->is_gid100() && (empty($bbs) || $bbs['display']))
			$this->message('版块已关闭', 'index.php');
		
		$bbs['master_name'] = array();
		$bbs['master'] = array_filter(explode(',', $bbs['master']));
		$bbs['master'][] = 1;
		foreach($bbs['master'] as $k=>$v) {
			if(!empty($v)) {
				$u = $this->user->get_user_by_uid($v, false);
				if(!empty($u)) $bbs['master_name'][] = $u['name'];
			}
		}
		
		//hook ctr_index_forum_before_siteinfo_asgn.php
		$this->tpl->assign('_site_sub_keywords', $bbs['name']);
		$this->tpl->assign('_site_sub_description', $bbs['name']);
		$this->tpl->assign('_site_sub_title', $bbs['name']);
	
		//hook ctr_index_forum_before_pages.php
		$pagesize = $limit = max(0, $this->confs['_conf_bbs_pagesize']);
		$page	= html::page('page');
		$count = $digest ? $this->talks->talks_of_digest_count($fid)
						: $this->db->count("talks-fid-$fid");
		
		$allstick_count = $this->talks->talks_of_allstick_count(false, $fid, $digest);
		$stick_count = $this->talks->talks_of_stick_count($fid, $digest);
		$count = $count - $allstick_count - $stick_count;
		
		$allstick_count = $this->talks->talks_of_allstick_count(($fid==0));
		$stick_count = $this->talks->talks_of_stick_count($fid);
		
		//hook ctr_index_forum_in_pages.php
		$digest_url = $digest? '-d-j':'';
		$pages = html::pages("index.php?index-forum-fid-$fid$digest_url.htm", $count, $page, $pagesize);
		
		$start = ($page-1)*$pagesize;
		//hook ctr_index_forum_before_list.php
		$talks = $this->talks->talks_list($fid, $start, $limit,$digest);
		$allstick = $this->talks->talks_of_allstick_list(0, $allstick_count, $fid);
		$sticks = $this->talks->talks_of_stick_list($fid,0,$stick_count);
	
		//hook ctr_index_forum_before_asgn.php
		$this->tpl->assign('pages', $pages);
		$this->tpl->assign('talks', $talks);
		$this->tpl->assign('sticks', $sticks);
		$this->tpl->assign('allstick', $allstick);
		$this->tpl->assign('digest', $digest);
		$this->tpl->assign('bbs', $bbs);
		$this->tpl->display('index_forum.tpl');
	}
	
	public function do_talk() {
		$tid = core::gpc('tid', 'G');
		//帖子
		//hook ctr_index_talk_show_talk_before.php
		$the_talk = $this->talks->talks_show($tid,true);
		if(empty($the_talk)) $this->message('帖子不存在', 'index.php');
		
		$the_talk['content'] = nl2br(str_replace("\n","\n\n",$the_talk['content']));
		$the_talk['content'] = str_replace("\t",'&nbsp;&nbsp;&nbsp;&nbsp;',$the_talk['content']);
		//hook ctr_index_talk_parse_content_before.php
		$this->parse->parse_face($the_talk['content']);
		$this->parse->parse_all_image_in_content($the_talk['content']);
		$this->parse->parse_all_files_in_content($the_talk['content']);
		//hook ctr_index_talk_parse_content_end.php
		
		$this->tpl->assign('_site_sub_keywords', $the_talk['title']);
		$this->tpl->assign('_site_sub_description', $the_talk['title']);
		$this->tpl->assign('_site_sub_title', $the_talk['title']);
		//作者
		//hook ctr_index_talk_parse_author_before.php
		$author  = $this->user->get_user_by_uid($the_talk['uid'], false);
		if(empty($author)) $this->message('作者不存在', 'index.php');
		$author = $this->user->user_format($author);
		$isonline = $this->user->is_online($author['laststamp']);
		//称号
		//hook ctr_index_talk_parse_utitle_before.php
		$utitle_arr = $this->conf->get_conf_arr(6);
		$utitle_level = $this->miscfunc->get_level_core($author['core']);
		$this->miscfunc->get_level_limit($utitle_level, $utitle_arr);
		$author['utitle'] = $utitle_arr[$utitle_level];
		//板块
		//hook ctr_index_talk_get_bbs_before.php
		$bbs = $this->forum->forum_get($the_talk['fid']);
		$bbs['master'] = array_filter(explode(',', $bbs['master']));
		$bbs['master'][] = 1;
		
		//所有版块
		//hook ctr_index_talk_forum_list_before.php
		$bbses = $this->forum->forum_list(1);
		
		//回复
		//hook ctr_index_talk_comm_list_before.php
		$limit = max(0, $this->confs['_conf_bbs_commentsize']);
		$replys = $this->comm->comm_list($tid, 0 , $limit);
		
		foreach($replys as $each_key => &$each_rep) {
			$each_rep['user'] = $this->user->get_user_by_uid($each_rep['uid'], false);
			if(empty($each_rep['user'])) {
				unset($replys[$each_key]);
			}
			//hook ctr_index_talk_in_parse_each_reply.php
			$each_rep['user'] = $this->user->user_format($each_rep['user']);
			$each_rep['isonline'] = $this->user->is_online($each_rep['user']['laststamp']);
			
			$utitle_level = $this->miscfunc->get_level_core($each_rep['user']['core']);
			$this->miscfunc->get_level_limit($utitle_level, $utitle_arr);
			$each_rep['user']['utitle'] = $utitle_arr[$utitle_level];
			
			$each_rep['content'] = nl2br(str_replace("\n","\n\n",$each_rep['content']));
			$this->parse->parse_face($each_rep['content']);
		}
		//hook ctr_index_talk_end.php
		$this->tpl->assign('the_talk', $the_talk);
		$this->tpl->assign('author', $author);
		$this->tpl->assign('isonline', $isonline);
		$this->tpl->assign('bbs', $bbs);
		$this->tpl->assign('bbses', $bbses);
		$this->tpl->assign('replys', $replys);
		$this->tpl->display('index_talk.tpl');
	}
	
	public function do_commlist() {
		$fid = intval(core::gpc('fid','G'));

		//hook ctr_index_commlist_start.php
		$the_talk = $this->talks->talks_show($fid);
		if(empty($the_talk)) $this->message('帖子不存在', 'index.php');
		if(isset($the_talk['content'])) unset($the_talk['content']);
		
		$this->tpl->assign('_site_sub_keywords', $the_talk['title']);
		$this->tpl->assign('_site_sub_description', $the_talk['title']);
		$this->tpl->assign('_site_sub_title', $the_talk['title']);
		
		//作者
		//hook ctr_index_commlist_get_author_before.php
		$author  = $this->user->get_user_by_uid($the_talk['uid'], false);
		if(empty($author)) $this->message('作者不存在', 'index.php');
		//称号
		//hook ctr_index_commlist_get_utitle_before.php
		$utitle_arr = $this->conf->get_conf_arr(6);
		
		//板块
		//hook ctr_index_commlist_get_bbs_before.php
		$bbs = $this->forum->forum_get($the_talk['fid']);
		$bbs['master'] = array_filter(explode(',', $bbs['master']));
		$bbs['master'][] = 1;
		
		//hook ctr_index_commlist_page_before.php
		$pagesize = $limit = max(0, $this->confs['_conf_bbs_commentsize']);
		$page	= html::page('page');
		$count = max(0, $the_talk['tj']);
		$pages = html::pages("index.php?index-commlist-fid-$fid.htm", $count, $page, $pagesize);
		$start = ($page-1)*$pagesize;
		$replys = $this->comm->comm_list($fid, $start , $limit, 1);
		//hook ctr_index_commlist_page_end.php
		
		foreach($replys as $each_key => &$each_rep) {
			$each_rep['user'] = $this->user->get_user_by_uid($each_rep['uid'], false);
			if(empty($each_rep['user'])) {
				unset($replys[$each_key]);
			}
			//hook ctr_index_commlist_in_foreach_reply.php
			$each_rep['user'] = $this->user->user_format($each_rep['user']);
			$each_rep['isonline'] = $this->user->is_online($each_rep['user']['laststamp']);
			
			$utitle_level = $this->miscfunc->get_level_core($each_rep['user']['core']);
			$this->miscfunc->get_level_limit($utitle_level, $utitle_arr);
			$each_rep['user']['utitle'] = $utitle_arr[$utitle_level];
			
			$each_rep['content'] = nl2br(str_replace("\n","\n\n",$each_rep['content']));
			$this->parse->parse_face($each_rep['content']);
		}
		//hook ctr_index_commlist_end.php
		$this->tpl->assign('the_talk', $the_talk);
		$this->tpl->assign('author', $author);
		$this->tpl->assign('pages', $pages);
		$this->tpl->assign('bbs', $bbs);
		$this->tpl->assign('replys', $replys);
		$this->tpl->display('index_comm.tpl');
	}
	
	//====================>need logined
	public function do_utalks() {
		$this->check_user_login_msg();
		
		//hook ctr_index_utalks_start.php
		$core_arr = $this->user->user_core();
		
		//hook ctr_index_utalks_page_before.php
		$pagesize = $limit = max(0, $this->confs['_conf_bbs_pagesize']);
		$page	= html::page('page');
		$count = $this->talks->talks_of_users_count($core_arr['uid']);
		$pages = html::pages('index.php?index-utalks.htm', $count, $page, $pagesize);
		$start = ($page-1)*$pagesize;
		$talks = $this->talks->talks_of_users_list($core_arr['uid'], $start, $limit);
		//hook ctr_index_utalks_page_end.php
		
		$this->tpl->assign('talks', $talks);
		$this->tpl->assign('pages', $pages);
		$this->tpl->display('user_talkslist.tpl');
	}
	
	public function do_ucomms() {
		$this->check_user_login_msg();
		
		//hook ctr_index_ucomms_start.php
		$core_arr = $this->user->user_core();
		
		//hook ctr_index_ucomms_page_before.php
		$pagesize = $limit = max(0, $this->confs['_conf_bbs_pagesize']);
		$page	= html::page('page');
		$count = $this->comm->comm_of_users_count($core_arr['uid']);
		$pages = html::pages('index.php?index-ucomms.htm', $count, $page, $pagesize);
		$start = ($page-1)*$pagesize;
		$comms = $this->comm->comm_of_users_list($core_arr['uid'], $start, $limit);
	
		//hook ctr_index_ucomms_page_end.php
		$this->tpl->assign('comms', $comms);
		$this->tpl->assign('pages', $pages);
		$this->tpl->display('user_commslist.tpl');
	}
	
	public function do_usubrpl() {
		$this->check_user_login_msg();
		
		//hook ctr_index_usubrpl_start.php
		$core_arr = $this->user->user_core();
		
		//hook ctr_index_usubrpl_page_before.php
		$pagesize = $limit = max(0, $this->confs['_conf_bbs_pagesize']);
		$page	= html::page('page');
		$count = $this->subrpl->subrpl_of_users_count($core_arr['uid']);
		$pages = html::pages('index.php?index-usubrpl.htm', $count, $page, $pagesize);
		$start = ($page-1)*$pagesize;
		$subrpl = $this->subrpl->subrpl_of_users_list($core_arr['uid'], $start, $limit);
	
		//hook ctr_index_usubrpl_page_end.php
		$this->tpl->assign('subrpl', $subrpl);
		$this->tpl->assign('pages', $pages);
		$this->tpl->display('user_subrpllist.tpl');
	}
	//==================>private
	private function is_gid100() {
		$gid = $this->user->user_groupid();
		//hook ctr_index_in_is_gid100.php
		if($gid == 100){
			return true;
		}
		return false;
	}

	//hook ctr_index_end.php
}
/*{TranslateTagEnd}*/

?>